﻿using MPic.Business.DAL;
using MPic.Business.Entity;
using MPic.Business.Enums;
using MPic.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MPic.Business.BLL
{
    public class FilesBLL
    {
        private FilesDAL filesDal = new FilesDAL(DbConnectionEnum.MPic);
        private static object locker = new object();
        private static FilesBLL _instance;
        public static FilesBLL Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (locker)
                    {
                        if (_instance == null)
                        {
                            _instance = new FilesBLL();
                        }
                    }
                }
                return _instance;
            }
        }

        /// <summary>
        /// 更新上传信息
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
        public bool Insert(int userId, int albumId, string imgUrl, string fileSize, string fileKey)
        {
            var files = new FilesEntity
            {
                UserId = userId,
                FileUrl = imgUrl,
                FileSize = fileSize,
                UploadDate = DateTime.Now,
                UploadIp = IPHelper.getIPAddr(),
                ClientVersion = "1.0.0",
                UploadWay = 5,
                UploadType = UploadTypeEnum.QiNiu.ToInt(),
                IsDel = false,
                FileKey = fileKey,
                AlbumId = albumId
            };
            //更新相册数量
            UserAlbumBLL.Instance.Update(albumId);
            //更新用户上传次数
            UsersBLL.Instance.Update(userId);
            //插入上传文件信息
            return filesDal.Insert(files) > 0;
        }

        public List<FilesEntity> List(int userId, int albumId)
        {
            return filesDal.GetList(new { UserId = userId, AlbumId = albumId }, "UploadDate DESC").ToList();
        }
    }
}
